﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NewsDiscoveryEngine;
using NewsMine.DataManagers;
using NewsMine.DomainObjects;
using System.Web;
using NewsDiscoveryEngine.Classification;
using System.Threading;
using NewsMine.Storage;
using System.IO;

namespace NewsMine.RepairMaster
{
    public partial class Menu_Identifier : Form
    {
        public Menu_Identifier()
        {
            InitializeComponent();
        }

        private void btnMenuTagsIdentify_Click(object sender, EventArgs e)
        {
            IdentifyTagsFromMenuUrls();
        }

        private void IdentifyAndSaveTagsForMenuItems()
        {
            // here we need to identify and also save the tags along with the menu item in the websiteInfo.
            // And also we need to save back to the websiteMaster database.

        }

      


        private void Mark_HOME_category_Where_domain_Url_in_MenuItem()
        {

            //todo: this method needs to be completed.


            var webSiteInfoStore = Defaults.WebSiteInfoMasterStore;

            webSiteInfoStore.Open();

            List<object> objs = webSiteInfoStore.Get(0, webSiteInfoStore.GetCount());

            if (objs == null || objs.Count <= 0)
                return;

            List<WebSiteMenu> menuItemsForCrawling = new List<WebSiteMenu>();
            Dictionary<string, object> sitesForUpdatingBackToDatabase = new Dictionary<string, object>();

            foreach (var obj in objs)
            {
                WebSiteInfo wbSiteInfo = (WebSiteInfo)obj;

                if (wbSiteInfo.MenuItemconfigs == null || wbSiteInfo.MenuItemconfigs.Count <= 0)
                    continue;

                bool isAtleastOneMenuItemIsScheduledForCrawling = false;
                foreach (var menuItem in wbSiteInfo.MenuItemconfigs)
                {
                    int refreshFrequencyInSeconds = NewsMine.Configuration.CommonConstants.DefaultNewsRefreshFrequencyInSeconds;

                    if (menuItem.NewsRefreshFrequencyInSeconds > 0)
                        refreshFrequencyInSeconds = menuItem.NewsRefreshFrequencyInSeconds;

                    if (menuItem.LastCrawledTimeGMT.AddSeconds(refreshFrequencyInSeconds) < DateTime.UtcNow)
                    {
                        menuItemsForCrawling.Add(menuItem);
                        isAtleastOneMenuItemIsScheduledForCrawling = true;
                    }
                }

                if (isAtleastOneMenuItemIsScheduledForCrawling == true)
                {
                    if (sitesForUpdatingBackToDatabase.Keys.Contains<string>(wbSiteInfo.Domain) == false)
                        sitesForUpdatingBackToDatabase.Add(wbSiteInfo.Domain, wbSiteInfo);
                }
            }

            webSiteInfoStore.ReplaceMultiple(sitesForUpdatingBackToDatabase);
            webSiteInfoStore.Close();

            //return menuItemsForCrawling;
        }

        private void IdentifyTagsFromMenuUrls()
        {
            /*
             * Get all the menu urls from the website master db
             * 
             * for each url split the url into segments
             * Ignore the domain name segment.
             * if the there is only website domain in the menu, we need to consder that menu url as home / topstories / head lines
             * 
             * We need to add all the idenfied items to Tags property. So that we can use them in the indexing process. 
             * 
             * */

            WebSiteInfoManager webSitInfoManager = new WebSiteInfoManager();

            var menuItems = WebSiteInfoManager.GetAllMenuItems();

            Dictionary<string, int> tagCounts = new Dictionary<string, int>();

            foreach (WebSiteMenu menu in menuItems)
            {
                MergeTagCountFromMenuUrl(ref tagCounts, menu);
            }

            var sortedDict = (from entry in tagCounts orderby entry.Value descending select entry)
                .ToDictionary(pair => pair.Key, pair => pair.Value);

            StringBuilder sb = new StringBuilder();
            StringBuilder sb1 = new StringBuilder();


            foreach (var item in sortedDict)
            {
                sb.AppendLine(item.Key + "\t" + item.Value);
                sb1.AppendLine(item.Key);
            }


            File.WriteAllText("C:\\KeywordsFromMenuItems.txt", sb.ToString());
            File.WriteAllText("C:\\KeywordsFromMenuItems1.txt", sb1.ToString());

            MessageBox.Show(tagCounts.Count.ToString());

        }

        private void MergeTagCountFromMenuUrl(ref Dictionary<string, int> tagCounts, WebSiteMenu menu)
        {
            List<string> tagsFromUrl = UrlBasedTagsDetectionClassifier.GetTagsFromUrl(menu.Url);

            tagsFromUrl.Add(menu.Title.Trim());
            // merge the resulted tags in the tags count dictionary.

            string tempTag = string.Empty;
            bool tempIsNumber = false;
            int tempNumber = 0;

            List<string> resultedTagsAfterInternalSplitInEachTag = new List<string>();
            char[] delimiters = new char[] { ' ', '-','_','/' };
            foreach (string tag in tagsFromUrl)
            {
                //resultedTagsAfterInternalSplitInEachTag.Add(tag);

                resultedTagsAfterInternalSplitInEachTag.AddRange(tag.Split(delimiters));

            }

            foreach (string tag in resultedTagsAfterInternalSplitInEachTag)
            {
                if (string.IsNullOrEmpty(tag))
                    continue;

                tempTag = tag.ToLower();

                tempIsNumber = false;
                tempNumber = 0;
                tempIsNumber = int.TryParse(tempTag, out tempNumber);

                if (tempIsNumber = true && tempNumber > 0)
                    continue;




                if (tagCounts.Keys.Contains(tempTag) == true)
                {
                    //if the item is already exists in the dictionary, we need to just increase the count.
                    tagCounts[tempTag] = tagCounts[tempTag] + 1;
                }
                else
                {
                    //if the item is not exists, we need to just included
                    tagCounts.Add(tempTag, 1);
                }
            }
        }

        // Process the html feed into staging area.

        private void btnReprocessToStagingIndex_Click(object sender, EventArgs e)
        {
            MessageBox.Show("nothing inside.");
        }

      

        private void btnBrowserToPicture_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www.google.com/");

            Thread.Sleep(5000);

            //getthe instance of the graphics from the control
            Graphics g = webBrowser1.CreateGraphics();

            //new bitmap object to save the image
            Bitmap bmp = new Bitmap(webBrowser1.Width, webBrowser1.Height);

            //Drawing control to the bitmap
            webBrowser1.DrawToBitmap(bmp, new Rectangle(0, 0, webBrowser1.Width, webBrowser1.Height));

            bmp.Save("c:\\browserImage.bmp");
            bmp.Dispose();
        }
    }
}
